package com.herench.bunch.clinic.convert;


import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.message.DSR_Q01;
import ca.uhn.hl7v2.model.v24.message.QRY_Q01;
import ca.uhn.hl7v2.model.v24.segment.DSP;
import ca.uhn.hl7v2.model.v24.segment.MSA;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.model.v24.segment.QRD;
import com.herench.bunch.clinic.handler.ProxyMsgCodeConstant;
import com.herench.bunch.clinic.support.ProxyMsgTemplate;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.dom4j.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * 锁号或取消锁号报文转换器
 */
public class ReservationOrCancelSourceConvert extends AbstractMessageConvert {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Document doToMessage(RequestMessage requestMessage) throws Exception {
        Document document = ProxyMsgTemplate.getInMsgTpl(ProxyMsgCodeConstant.RESERVATION_OR_CANCEL_SOURCE_CODE);
        QRY_Q01 hl7Message = (QRY_Q01) requestMessage.getHl7Message();
        QRD qrd = hl7Message.getQRD();
        CE[] ces = qrd.getQrd9_WhatSubjectFilter();
        for (CE ce : ces) {
            String value = ce.getText().getValue();
            document.getRootElement().addElement(ce.getCe1_Identifier().getValue()).addText(value!=null?value:"");
        }
        String res = document.asXML();
        logger.info(requestMessage.getMsgCode() + " to HIS XML = "+ res);
        return document;
    }

    @Override
    public Message doFromMessage(String message) throws Exception {
        Assert.notNull(message,"message 为空.");
        logger.info("200110 from HIS XML = "+ message);
        Document doc = ProxyMsgTemplate.getOutMsgTpl(message);
        DSR_Q01 dsr = new DSR_Q01();
        dsr.initQuickstart("DSR_Q01", "Q01", "A");
        MSH msh = dsr.getMSH();
        initResponseMessageMSH(msh);
        MSA msa = dsr.getMSA();
        msa.getErrorCondition().getCe1_Identifier().setValue(doc.getRootElement().elementTextTrim("IsSuccessed"));
        msa.getErrorCondition().getCe2_Text().setValue(doc.getRootElement().elementTextTrim("ErrorMsg"));
        int index = 0;
        DSP dsp1 = dsr.insertDSP(index++);
        dsp1.getDsp3_DataLine().setValue("DivisionID="+doc.getRootElement().elementTextTrim("DivisionID"));
        DSP dsp2 = dsr.insertDSP(index++);
        dsp2.getDsp3_DataLine().setValue("RemainsNum="+doc.getRootElement().elementTextTrim("RemainsNum"));
        return dsr;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QRY_Q01 qry = new QRY_Q01();
        qry.initQuickstart("QRY_Q01", "Q01", "A");
        MSH msh = qry.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("200110");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        QRD qrd = qry.getQRD();

        int index = 0;
        CE ce = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce.getCe1_Identifier().setValue("HISPatientID");
        ce.getCe2_Text().setValue("40021810311243270555");

        CE ce1 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce1.getCe1_Identifier().setValue("VisitCode");
        ce1.getCe2_Text().setValue("000023|2018-10-31|P");

        CE ce2 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce2.getCe1_Identifier().setValue("DoctorCode");
        ce2.getCe2_Text().setValue("");

        CE ce3 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce3.getCe1_Identifier().setValue("DeptCode");
        ce3.getCe2_Text().setValue("01010204");

        CE ce4 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce4.getCe1_Identifier().setValue("APW");
        ce4.getCe2_Text().setValue("P");

        CE ce5 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce5.getCe1_Identifier().setValue("VisitDate");
        ce5.getCe2_Text().setValue("2018-10-31");

        CE ce6 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce6.getCe1_Identifier().setValue("BusinessFrom");
        ce6.getCe2_Text().setValue("3");

        CE ce7 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce7.getCe1_Identifier().setValue("LockType");
        ce7.getCe2_Text().setValue("0");

        CE ce8 = qrd.insertQrd9_WhatSubjectFilter(index++);
        ce8.getCe1_Identifier().setValue("DivisionID");
        ce8.getCe2_Text().setValue("4002S1810311732482010");

        return qry;
    }


}
